<?php

namespace app\command;

use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use Workerman\Worker;

class Websocket extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('admsocket')
            ->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
            ->addOption('mode', 'm', Option::VALUE_OPTIONAL, 'Run the workerman server in daemon mode.')
            ->setDescription('the workerman command');
    }

    protected function execute(Input $input, Output $output)
    {
        $action = $input->getArgument('action');
        $mode = $input->getOption('mode');

        // 重新构造命令行参数,以便兼容workerman的命令
        global $argv;

        $argv = [];
        array_unshift($argv, 'think', $action);
        if ($mode == 'd') {
            $argv[] = '-d';
        } else if ($mode == 'g') {
            $argv[] = '-g';
        }

        switch (strtolower($mode)) {
            case 'business':
                $this->business();
                break;
            case 'gateway':
                $this->gateway();
                break;
            case 'register':
                $this->register();
                break;
            case 'businessandgateway':
                $this->business();
                $this->gateway();
                break;
            default:
                $this->business();
                $this->gateway();
                $this->register();
        }
        Worker::runAll();
    }

    protected function business()
    {
        // bussinessWorker 进程
        $worker = new BusinessWorker();
        // worker名称
        $worker->name = config('gateway.business_name', 'BusinessWorker');
        // bussinessWorker进程数量
        $worker->count = (int)config('gateway.business_count', 4);;
        // 服务注册地址
        $worker->registerAddress = config('gateway.register_address', '127.0.0.1:1236');

        $worker->eventHandler = config('gateway.business_event_handler');
    }

    protected function gateway()
    {
        // gateway 进程
        $gateway = new Gateway(
            config('gateway.gateway_socket_name'),
            config('gateway.gateway_context_option')
        );
        // 设置名称，方便status时查看
        $gateway->name = config('gateway.gateway_name');
        // 设置进程数，gateway进程数建议与cpu核数相同
        $gateway->count = (int)config('gateway.gateway_count', 4);
        // 分布式部署时请设置成内网ip（非127.0.0.1）
        $gateway->lanIp = config('gateway.gateway_lanIp');;
        // 内部通讯起始端口。假如$gateway->count=4，起始端口为2300
        // 则一般会使用2300 2301 2302 2303 4个端口作为内部通讯端口
        $gateway->startPort = (int)config('gateway.gateway_start_port', 2300);
        // 心跳间隔
        $gateway->pingInterval = 30;
        // 心跳数据
        $gateway->pingData = '{"type":"ping"}';
        // 服务注册地址
        $gateway->registerAddress = config('gateway.register_connection');
    }

    protected function register()
    {
        $register = new Register(config('gateway.register'));
        $register->count = (int)config('gateway.register_count', 1);
    }

}